﻿<cfscript>

/** 
* Security Frameworks - groupFilter
* 
* @extends "MachII.framework.Listener"
* 
* @displayname "acadmicManager.listeners.system.userListener" 
* @hint "system - user infomation operation"
* 
* @Output false
* 
* @depends "sessionAdvice, stringAdvice"
*/ 

component {
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 
	

	/**
	* @hint "更新用户注册信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateUserProfile( required MachII.framework.Event event ) output=false {
		
		var userId = event.getArg("UID");
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT a.user_name
					FROM t_user a 
					WHERE a.user_id = :userId ";
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="userId", value=userId, cfsqltype="cf_sql_varchar" );
			
			var rs_user = queryObj.execute( sql=sql ).getResult();
			
			var UserName = rs_user.user_name;

			var userName = trim( event.getArg("UserName") );
			if ( len(userName) and userName neq rs_user.user_name ) {
				UserName = userName ;
			}
			var userEmail = trim( event.getArg("EmailAddress") );
			
			if ( len( userEmail ) and getstringAdvice().isEmail( userEmail ) ) {
				
				/* 检查电子邮件唯一性 */
				
				/* Database Operation */
				sql = "SELECT user_id FROM t_user WHERE user_email = :userEmail ";
				
				queryObj = new Query( datasource=application.dnsMaster );
				queryObj.addParam( name="userEmail", value=userEmail, cfsqltype="cf_sql_varchar" );
				
				var rs_check = queryObj.execute( sql=sql ).getResult();
				
				if ( rs_check.recordCount eq 0 or rs_check.user_id eq userId ) {
					
					/* Database Operation */
					sql = "UPDATE t_user 
							SET 
								user_email = :userEmail, 
								user_name = :UserName 
							WHERE 
								user_id = :userId ";
					
					queryObj = new Query( datasource=application.dnsMaster );
					queryObj.addParam( name="userEmail", value=userEmail, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="UserName", value=UserName, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="userId", value=userId, cfsqltype="cf_sql_varchar" );

					queryObj.execute( sql=sql );

				}

			}

		}

	}


	/**
	* @hint "更新用户联系信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateUserContract( required MachII.framework.Event event ) output=false {
		
		var userId = getsessionAdvice().getAutherUserID();
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			var userName = trim( event.getArg("UserName") );
			var userEmail = trim( event.getArg("EmailAddress") );

			if ( len( userEmail ) and getstringAdvice().isEmail( userEmail ) ) {
				
				 /* Database Operation */
				 sql = "UPDATE t_user 
						 SET 
							 user_name = :userName, 
							 user_email = :userEmail 
						 WHERE 
							 user_id = :userId ";

				 queryObj = new Query( datasource=application.dnsMaster );
				 queryObj.addParam( name="userEmail", value=userEmail, cfsqltype="cf_sql_varchar" );
				 queryObj.addParam( name="userName", value=userName, cfsqltype="cf_sql_varchar" );
				 queryObj.addParam( name="userId", value=userId, cfsqltype="cf_sql_varchar" );

				 queryObj.execute( sql=sql );

			}

		}

	}

}

</cfscript>